package com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository;

import android.support.annotation.NonNull;

import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.base.Result;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Event;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Organization;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.PeoplesModel;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.User;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.resultModel.EventResultModel;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.resultModel.SignUpResult;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MultipartBody;
import retrofit2.http.Body;
import retrofit2.http.Path;

/**
 * Created by ZengYan on 2018/4/14.
 *
 * @desc :
 */
public class MainRepository implements MainService {

    private static MainRepository INSTANCE = null;
    private final MainService mRemoteDataSource;
    private final MainService mLocalDataSource;
    private String mStuNum = "102430098034";


    public static MainRepository getInstance(@NonNull MainService mRemoteDataSource, @NonNull MainService mLocalDataSource) {
        if (INSTANCE == null) {
            INSTANCE = new MainRepository(mRemoteDataSource, mLocalDataSource);
        }
        return INSTANCE;
    }


    public MainRepository(@NonNull MainService mRemoteDataSource, @NonNull MainService mLocalDataSource) {
        this.mRemoteDataSource = mRemoteDataSource;
        this.mLocalDataSource = mLocalDataSource;
    }


    @Override
    public Observable<Result<User>> login(@Body Map<String, String> loginMap) {
        return mLocalDataSource
                .login(loginMap)
                .onErrorResumeNext(mRemoteDataSource.login(loginMap));
    }

    public static void destoryInstance() {
        INSTANCE = null;
    }

    @Override
    public Observable<Result<List<Organization>>> getMyOrgs(String stuNum) {
        return mLocalDataSource
                .getMyOrgs(stuNum)
                .onErrorResumeNext(mRemoteDataSource.getMyOrgs(stuNum));
    }

    @Override
    public Observable<Result<PeoplesModel>> getPeoplesInOrg(int id) {
        return mLocalDataSource
                .getPeoplesInOrg(id)
                .onErrorResumeNext(mRemoteDataSource.getPeoplesInOrg(id));
    }

    @Override
    public Observable<Result<Organization>> getOrgById(int id) {
        return mRemoteDataSource.getOrgById(id);
    }

    @Override
    public Observable<Result<User>> getProfileInfo(String stunum) {
        return mLocalDataSource
                .getProfileInfo(stunum)
                .onErrorResumeNext(mRemoteDataSource.getProfileInfo(stunum));

    }

    @Override
    public Observable<Result<Event>> createEvent(Map<String, String> map) {
        return mRemoteDataSource.createEvent(map);
    }

    @Override
    public Observable<Result<List<Event>>> getEventsFromOrg(int org_id) {
        return mRemoteDataSource.getEventsFromOrg(org_id);
    }

    @Override
    public Observable<Result<List<EventResultModel>>> getEventsAll(@Path("stunum") String stunum) {
        return mRemoteDataSource.getEventsAll(stunum);
    }

    @Override
    public Observable<Result<SignUpResult>> signUp(Map<String, String> map) {
        return mRemoteDataSource.signUp(map);
    }

    @Override
    public Observable<Result<List<Event>>> getEventsIJoinUp(String stuNu) {
        return mRemoteDataSource.getEventsIJoinUp(stuNu);
    }

    @Override
    public Observable<Result<List<User>>> searchUser(String stunum, String message) {
        return mRemoteDataSource.searchUser(stunum,message);
    }

    @Override
    public Observable<Result<User>> updateUserImage(MultipartBody.Part file, String stunum, String type) {
        return mRemoteDataSource.updateUserImage(file,stunum,type);
    }


}
